package se.webbzon.boltzmann.game.object.spawner;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.game.character.CharacterException;
import se.webbzon.boltzmann.game.object.dialogue.DialogueVoice;
import se.webbzon.boltzmann.game.object.npc.FriendlyAI;
import se.webbzon.boltzmann.game.object.npc.FriendlyNPC;
import se.webbzon.boltzmann.npc.NPCDuty;
import se.webbzon.boltzmann.object.unknown.Unknown;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class FriendlyNPCSpawner extends AbstractNPCSpawner {

	// The name of this deflatable world object
	public static final String worldObjectName = "friendlyNPCSpawner";
	
	// The artificial intelligence enumeration of the spawned character
	private FriendlyAI ai;
	
	// The voice of the spawned character
	private DialogueVoice voice;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new friendly non-player character spawner which
	 * spawner characters with the provided character texture set 
	 * and artificial intelligence. A unique id must be provided
	 * to the spawner upon creation. **/
	public FriendlyNPCSpawner(	long id, String key,
								String characterTextureSetName, 
								FriendlyAI ai,
								DialogueVoice voice) {
		super(worldObjectName, id, key, characterTextureSetName);
		this.ai = ai;
		this.voice = voice;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the artificial intelligence enumeration of the 
	 * spawned character. **/
	public FriendlyAI getAI() {
		return ai;
	}
	
	/** Returns the artificial intelligence enumeration of the 
	 * spawned character. **/
	public void setAI(FriendlyAI ai) {
		this.ai = ai;
	}
	
	/** Returns the voice of the spawned character. **/
	public DialogueVoice getVoice() {
		return voice;
	}
	
	/** Sets the voice of the spawned character. **/
	public void setVoice(DialogueVoice voice) {
		this.voice = voice;
	}

	@Override public WorldObjectDeflation getDeflation() {
		return new FriendlyNPCSpawnerDeflation(0,this);
	}

	@Override public WorldObject spawn(AbstractEnvironment env) {
		WorldStaticObject instance;
		NPCDuty d = null;
		double dy = 0.5;
		try {
			FriendlyNPC npc = new FriendlyNPC(getCharacterTextureSetName(),ai,voice);
			d = npc.getDuty();
			instance = npc;
		} catch (CharacterException e) {
			instance = new Unknown();
		}
		
		instance.setPosition(getX(),getY() + dy,getZ());
		instance.setSize(getWidth(), getHeight(), getDepth());
		instance.setRotation(getEX(), getEY(), getEZ(), getAngle());
		if (d != null)
			d.setOrigin(getX(), getY(), getZ());
		
		return instance;
	}

}
